$(function () {
    //新增按钮触发
    $('#btn_fphb_add_inport').click(function(){
        var data = $('#fphb_import_content_datagrid').datagrid('selectRow',2);
        $('#fphb_import_content_datagrid').datagrid('appendRow',data);
    });

    var options = {
        toolbar: "#fphb_import_content_toolbar",
        fitColumns: true,
        fit: true,
        singleSelect: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        rownumbers: true,
        showFooter: true,
        autoRowHeight: true,
        nowrap: false,
        columns: [
            [
                {field: 'voucherItemData.zy', title: '摘要', width: 50,editor: {type: 'text', options: {required: true}}},
                {field: 'voucherItemData.km', title: '科目', width: 50, editor: {type: 'text', options: {required: true}}},
                {field: 'voucherItemData.jfje', title: '借方', width: 30,editor: {type: 'numberbox', options: {min: 0, groupSeparator: ',', precision: 4, fit: true}}},
                {field: 'voucherItemData.dfje', title: '贷方', width: 30, editor: {type: 'numberbox', options: {min: 0, groupSeparator: ',', precision: 4, fit: true}}},
                {field: 'voucherItemData.fzhs', title: '辅助核算', editable:true,width: 50, align: 'center', formatter: formatColumn},
                {title: "操作",width:10,align:'center',field:"id",formatter:function(value,row,index){
                    return '<a name="deleteSelect" val="'+value+'"></a>';
                }}
            ]

        ],
        onClickCell:function(rowIndex, field, value){
            if(field=="id"){
                $('#fphb_import_content_datagrid').datagrid('deleteRow',rowIndex);
            }
        },
        onSelect: function (rowIndex, rowData) {
           $(this).datagrid('beginEdit', rowIndex);
            $("[name='deleteSelect']").linkbutton({
                plain:true,
                iconCls:'icon-cancel'
            });
        },
        url: $.webPath() + '/cwgl/fphb/findfphbfl',
        queryParams: {jsdhbId: $("#fphb_import_content_toolbar [name='jsdhbId']").val()}
    };

    var grid = $('#fphb_import_content_datagrid').datagrid(options);

    function formatColumn(value, row, index) {
        if (typeof(value) == "undefined" || value.length == 0)
            return "";
        var items = value.split("；");
        var target = ["客户", "供应商", "部门", "职员", "数量"];
        for (var i = 0; i < items.length; i++) {
            var temp = $.inArray(items[i], target);
            if (temp == -1)
            //return '<a name="tip" tooltip="系统目前只支持客户,供应商,部门,职员,数量等5种辅助核算!">' + value + '</a>';
                return '<span title="系统目前只支持客户,供应商,部门,职员,数量等5种辅助核算!" style="color:red;font-weight:bolder;">' + value + '</span>';
        }
        return   '<a name="edit" href="javascript:void(0)"  onclick="fphbDialog(\'' + value + '\',' + index + ')">'+value+'</a>';
//        temp += row["gysdm"] == "" ? "" : "供应商:" + row["gysdm"] + "_" + row["gysdmmc"];
//        temp += row["bmdm"] == "" ? "" : "部门:" + row["bmdm"] + "_" + rrow["bmdmmc"];
//        temp += row["zydm"] == "" ? "" : "职员:" + row["zydm"] + "_" + row["zyxm"];
//        temp += row["sl"] == "" ? "" : "数量:" + row["sl"];

    }
});

function fphbDialog(value, index) {
    var items = null;
    var win = $.window({
        title: "辅助核算项",
        iconCls: "icon-add",
        width: 300,
        height: 230,
        cache: false,
        modal: true,
        contents: '<div class="textcon"><table id="fzhs_window" style="width:100%" class="tb"><col width="20%"/><col width="80%"/></table></div>',
        onComplete: function () {
            var gsId = 1;
            items = value.split("；");
            var itemsArray = new Array();
            if ($.inArray("客户", items) != -1)
                itemsArray.push("<tr><td>客户</td><td><input id='khdm' type='text' class='input-text'/></td></tr>");
            if ($.inArray("供应商", items) != -1)
                itemsArray.push("<tr><td>供应商</td><td><input id='gysdm' type='text' class='input-text'/></td></tr>");
            if ($.inArray("部门", items) != -1)
                itemsArray.push("<tr><td>部门</td><td><input id='bmdm' type='text' class='input-text'/></td></tr>");
            if ($.inArray("职员", items) != -1)
                itemsArray.push("<tr><td>职员</td><td><input id='zydm' type='text' class='input-text'/></td></tr>");
            if ($.inArray("数量", items) != -1)
                itemsArray.push("<tr><td>数量</td><td><input id='sl' type='text' class='input-text'/></td></tr>");
            $(this).find("#fzhs_window").append(itemsArray.join(""));
            var row = $('#fphb_import_content_datagrid').datagrid("getData");
            row = row["rows"][index];
            if ($.inArray("客户", items) != -1) {
                $(this).find("#khdm").combobox({
                    valueField: 'kmdm',
                    textField: 'qyqc',
                    required: true,
                    url: $.webPath() + '/cwgl/kmys/ljkmdmys/findKmdm?nbgsId=' + gsId + "&type=1",
                    formatter: function (row) {
                        var opts = $(this).combobox('options');
                        return row[opts.valueField] + "   " + row[opts.textField];
                    }
                });
                $(this).find("#khdm").combobox("setValue", row["khdm"]);
            }
            if ($.inArray("供应商", items) != -1) {
                $(this).find("#gysdm").combobox({
                    valueField: 'kmdm',
                    textField: 'qyqc',
                    required: true,
                    url: $.webPath() + '/cwgl/kmys/ljkmdmys/findKmdm?nbgsId=' + gsId + "&type=0",
                    formatter: function (row) {
                        var opts = $(this).combobox('options');
                        return row[opts.valueField] + "   " + row[opts.textField];
                    }
                });
                $(this).find("#gysdm").combobox("setValue", row["gysdm"]);
            }
            if ($.inArray("部门", items) != -1) {
                $(this).find("#bmdm").combobox({
                    valueField: 'kmdm',
                    textField: 'bmqc',
                    required: true,
                    url: $.webPath() + '/organ/gsjg/findKmdm?gsjgId=' + gsId,
                    formatter: function (row) {
                        var opts = $(this).combobox('options');
                        return row[opts.valueField] + "   " + row[opts.textField];
                    }
                });
                $(this).find("#bmdm").combobox("setValue", row["bmdm"]);
            }
            if ($.inArray("职员", items) != -1) {
                $(this).find("#zydm").combobox({
                    valueField: 'kmdm',
                    textField: 'xm',
                    required: true,
                    url: $.webPath() + '/sys/yg/findKmdm?zggsId=' + gsId,
                    formatter: function (row) {
                        var opts = $(this).combobox('options');
                        return row[opts.valueField] + "   " + row[opts.textField];
                    }
                });
                $(this).find("#zydm").combobox("setValue", row["zydm"]);
            }
            if ($.inArray("数量", items) != -1) {
                $(this).find("#sl").numberbox({
                    required: true,
                    min: 0,
                    precision: 4
                });
                $(this).find("#sl").numberbox("setValue", row["sl"]);
            }
        },
        buttons: [
            {
                text: '确定',
                handler: function () {
                    var valid = true;
                    var row = $('#fphb_import_content_datagrid').datagrid("getSelected");
                    if ($.inArray("客户", items) != -1) {
                        valid = valid && $(win).find("#khdm").combobox("isValid");
                        row["voucherItemData.khdm"] = $(win).find("#khdm").combobox("getValue");
                        row["voucherItemData.khdmmc"] = $(win).find("#khdm").combobox("getText");
                    }
                    if ($.inArray("供应商", items) != -1) {
                        valid = valid && $(win).find("#gysdm").combobox("isValid");
                        row["voucherItemData.gysdm"] = $(win).find("#gysdm").combobox("getValue");
                        row["voucherItemData.gysdmmc"] = $(win).find("#gysdm").combobox("getText");
                    }
                    if ($.inArray("部门", items) != -1) {
                        valid = valid && $(win).find("#bmdm").combobox("isValid");
                        row["voucherItemData.bmdm"] = $(win).find("#bmdm").combobox("getValue");
                        row["voucherItemData.bmdmmc"] = $(win).find("#bmdm").combobox("getText");
                    }
                    if ($.inArray("职员", items) != -1) {
                        valid = valid && $(win).find("#zydm").combobox("isValid");
                        row["voucherItemData.zydm"] = $(win).find("#zydm").combobox("getValue");
                        row["voucherItemData.zyxm"] = $(win).find("#zydm").combobox("getText");
                    }
                    if ($.inArray("数量", items) != -1) {
                        valid = valid && $(win).find("#sl").numberbox("isValid");
                        row["voucherItemData.sl"] = $(win).find("#sl").val();
                    }
                    if (valid) {
                        $('#fphb_import_content_datagrid').edatagrid("acceptChanges");
                        $(win).window("close");
                    }
                }
            }
        ]
    });
}

